package com.weather.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weather.domain.Quote;
import com.weather.service.QuoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Quote 控制器
 */
@RestController
@RequestMapping("/quote")
public class QuoteController {

    @Autowired
    private QuoteService quoteService;

    /**
     * 新增 Quote
     */
    @PostMapping("/add")
    public String addQuote(@RequestBody Quote quote) {
        boolean result = quoteService.save(quote);
        return result ? "新增成功" : "新增失败";
    }

    /**
     * 删除 Quote（根据ID）
     */
    @DeleteMapping("/delete/{id}")
    public String deleteQuote(@PathVariable Long id) {
        boolean result = quoteService.removeById(id);
        return result ? "删除成功" : "删除失败";
    }

    /**
     * 修改 Quote
     */
    @PutMapping("/update")
    public String updateQuote(@RequestBody Quote quote) {
        boolean result = quoteService.updateById(quote);
        return result ? "修改成功" : "修改失败";
    }

    /**
     * 查询单条 Quote（根据ID）
     */
    @GetMapping("/get/{id}")
    public Quote getQuoteById(@PathVariable Long id) {
        return quoteService.getById(id);
    }

    /**
     * 查询列表（分页）
     */
    @GetMapping("/list")
    public Page<Quote> listQuotes(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        Page<Quote> page = new Page<>(pageNum, pageSize);
        return quoteService.page(page, new QueryWrapper<>()); // 简单分页查询，不加条件
    }
}

